<!DOCTYPE html>
<!-- This test cannot be upstreamed to WPT because it asserts internal
  implementation details using non-standard API that is only available via
  Chromium's content_shell. It should be maintained only in so far as those
  implementation details need to be held stable; assertions that can be
  expressed using standard interfaces should be added to the equivalent version
  of this test in the Web Platform Tests project. -->
<title>Service Worker: Redirected response URL list (uses Chromium-internal API)</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/get-host-info.js?pipe=sub"></script>
<script src="resources/test-helpers.js"></script>
<script>
// Tests redirect behavior. It calls fetch_method(url, fetch_option) and tests
// the resulting response against the expected values. It also adds the
// response to |cache| and checks the cached response matches the expected
// values.
//
// |options|: a dictionary of parameters for the test
// |options.url|: the URL to fetch
// |options.fetch_option|: the options passed to |fetch_method|
// |options.fetch_method|: the method used to fetch. Useful for testing an
//                         iframe's fetch() vs. this page's fetch().
// |options.cache|: A Cache to add the response to
// |options.expected_url_list|: an array of string values describing the
//                              internal URL list; this information is not
//                              available via a standard API
function redirected_test(options) {
  return options.fetch_method.call(null, options.url, options.fetch_option).then(response => {
        var cloned_response = response.clone();
        assert_array_equals(
            self.internals.getInternalResponseURLList(response),
            options.expected_url_list,
            'The URL list of response must match. URL: ' + options.url);
        assert_array_equals(
            self.internals.getInternalResponseURLList(cloned_response),
            options.expected_url_list,
            'The URL list of cloned response must match. URL: ' + options.url);
        return options.cache.put(options.url, response);
      })
    .then(_ => options.cache.match(options.url))
    .then(response => {
        assert_array_equals(
            self.internals.getInternalResponseURLList(response),
            options.expected_url_list,
            'The URL list of response in CacheStorage must match. URL: ' +
            options.url);
      });
}

var host_info = get_host_info();
var REDIRECT_URL = host_info['HTTP_ORIGIN'] +
                   '/serviceworker/resources/redirect.php?Redirect=';
var TARGET_URL = host_info['HTTP_ORIGIN'] +
                 '/serviceworker/resources/simple.txt';
var REDIRECT_TO_TARGET_URL = REDIRECT_URL + encodeURIComponent(TARGET_URL);
var frame;
var cache;
var setup;

promise_test(t => {
    var SCOPE = 'resources/blank.html?redirected-response';
    var SCRIPT = 'resources/fetch-rewrite-worker.js';
    var CACHE_NAME = 'serviceworker/redirected-response';
    setup = new Promise(function(resolve) {
          assert_true(!!self.internals, 'Chromium "internals" are exposed.');
          resolve();
        })
      .then(() => service_worker_unregister_and_register(t, SCRIPT, SCOPE))
      .then(registration => {
          promise_test(function() {
              return registration.unregister();
            }, 'restore global state (service worker registration)');

          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(_ => self.caches.open(CACHE_NAME))
      .then(c => {
          cache = c;

          promise_test(function() {
              return self.caches.delete(CACHE_NAME);
            }, 'restore global state (caches)');

          return with_iframe(SCOPE);
        })
      .then(f => {
          frame = f;

          add_completion_callback(function() {
              f.remove();
            });
        });
      return setup;
  }, 'initialize global state (service worker registration and caches)');

// ===============================================================
// Tests for requests that are out-of-scope of the service worker.
// ===============================================================
promise_test(t => setup
  .then(() => redirected_test({url: TARGET_URL,
                               fetch_option: {},
                               fetch_method: self.fetch,
                               cache: cache,
                               expected_url_list: [TARGET_URL]})),
  'mode: "follow", non-intercepted request, no server redirect');

promise_test(t => setup
  .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL,
                               fetch_option: {},
                               fetch_method: self.fetch,
                               cache: cache,
                               expected_url_list: [REDIRECT_TO_TARGET_URL, TARGET_URL]})),
  'mode: "follow", non-intercepted request');

promise_test(t => setup
  .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL + '&manual',
                               fetch_option: {redirect: 'manual'},
                               fetch_method: self.fetch,
                               cache: cache,
                               expected_url_list: [REDIRECT_TO_TARGET_URL + '&manual']})),
  'mode: "manual", non-intercepted request');

promise_test(t => setup
  .then(() => redirected_test({url: './?url=' + encodeURIComponent(TARGET_URL),
                               fetch_option: {},
                               fetch_method: frame.contentWindow.fetch,
                               cache: cache,
                               expected_url_list: [TARGET_URL]})),
  'mode: "follow", no mode change, no server redirect');

// =======================================================
// Tests for requests that are in-scope of the service worker. The service
// worker returns a redirected response.
// =======================================================
promise_test(t => setup
  .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
                                      '&original-redirect-mode=follow',
                               fetch_option: {redirect: 'follow'},
                               fetch_method: frame.contentWindow.fetch,
                               cache: cache,
                               expected_url_list: [REDIRECT_TO_TARGET_URL, TARGET_URL]})),
  'mode: "follow", no mode change');

promise_test(t => setup
  .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
                                       '&original-redirect-mode=manual&redirect-mode=manual',
                               fetch_option: {redirect: 'manual'},
                               fetch_method: frame.contentWindow.fetch,
                               cache: cache,
                               expected_url_list: [REDIRECT_TO_TARGET_URL]})),
  'mode: "manual", no mode change');
</script>
